package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.dto.MessageQueryDTO;
import com.zatech.cgnci.project.dto.MessageSendDTO;
import com.zatech.cgnci.project.entity.CgnciMessageDO;
import com.zatech.cgnci.project.entity.CgnciMessageReceiverDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.mapper.CgnciMessageMapper;
import com.zatech.cgnci.project.mapper.CgnciMessageReceiverMapper;
import com.zatech.cgnci.project.service.CgnciMessageService;
import com.zatech.cgnci.project.vo.MessageCountVO;
import com.zatech.cgnci.project.vo.MessageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CgnciMessageServiceImpl implements CgnciMessageService {
    @Autowired
    private  CgnciMessageMapper messageMapper;

    private final CgnciMessageReceiverMapper receiverMapper;
    
    @Override
    public IPage<MessageVO> pageMessage(MessageQueryDTO queryDTO) {
        UserEntity user = UserContext.getUser();
        return messageMapper.pageMessage(queryDTO.toPage(), queryDTO, user.getUsercode());
    }
    
    @Override
    public MessageVO getDetail(Long messageId) {
        UserEntity user = UserContext.getUser();
        MessageVO message = messageMapper.getDetail(messageId, user.getUsercode());
        if(message!=null) {
            message.setSendToUser(user.getNameAndCode());
            //标记为已读
            if("N".equals(message.getReadStatus())){
                receiverMapper.updateReadStatus(messageId, user.getUsercode());
            }
        }
        
        return message;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void markAsRead(List<Long> messageIds) {
        if(CollectionUtils.isEmpty(messageIds)) {
            return;
        }
        UserEntity user = UserContext.getUser();
        receiverMapper.batchUpdateReadStatus(messageIds, user.getUsercode());
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class) 
    public void clearAll() {
        UserEntity user = UserContext.getUser();
        receiverMapper.deleteByReceiver(user.getUsercode());
    }
    
    @Override
    public MessageCountVO getCount() {
        UserEntity user = UserContext.getUser();
        return messageMapper.getCount(user.getUsercode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendMessage(MessageSendDTO sendDTO) {
        UserEntity user = UserContext.getUser();

        // 1. 保存消息主表
        CgnciMessageDO message = new CgnciMessageDO();
        BeanUtils.copyProperties(sendDTO, message);
        message.setSender(user.getUsercode());
        message.setSendTime(new Date());
        messageMapper.insert(message);
        
        // 2. 保存接收记录
        for(String receiver : sendDTO.getReceivers()) {
            CgnciMessageReceiverDO receiverDO = new CgnciMessageReceiverDO();
            receiverDO.setMessageId(message.getMessageId());
            receiverDO.setReceiver(receiver);
            receiverMapper.insert(receiverDO);
        }
    }
    
    //系统消息发送接口
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendSystemMessage(MessageSendDTO sendDTO) {

        // 1. 保存消息主表
        CgnciMessageDO message = new CgnciMessageDO();
        BeanUtils.copyProperties(sendDTO, message);
        message.setSender("系统");
        message.setSendTime(new Date());
        messageMapper.insert(message);

        // 2. 保存接收记录
        for(String receiver : sendDTO.getReceivers()) {
            CgnciMessageReceiverDO receiverDO = new CgnciMessageReceiverDO();
            receiverDO.setMessageId(message.getMessageId());
            receiverDO.setReceiver(receiver);
            receiverMapper.insert(receiverDO);
        }
    }
} 